//引入axios
import axios from 'axios'
import baseUrl from './baseUrl'
import store from '@/store/Store'

let request = axios.create({
  baseURL: baseUrl,
  timeout: 15000,
})
// 请求拦截
request.interceptors.request.use(config => {
  localStorage.getItem('userCenter_token') ? config.headers.token = localStorage.getItem('userCenter_token') : ''
  config.cancelToken = new axios.CancelToken(cancel => store.commit('userStore/addHttpRequestList', cancel))
  return config
}, err => {
  err.response = { code: 500, msg:'请求失败，请检查网络' }
  return err.response
})
// 响应拦截
request.interceptors.response.use(response => {
  return response.data
}, err => {
  if(err.message === 'suspend') {
    return new Promise(() => {})
  }else if (err.message.indexOf('timeout') !== -1) {
    err.response = { code: 500, msg:'响应超时，请检查网络' }
    return err.response
  }
  return Promise.reject(err)
})

export default request

// 登录
export function login(formData){
  return request.post('/login',formData)
}
// 验证权限
export function auth(){
  return request.post('/auth')
}
// 修改账户信息
export function userEdit(formData){
  return request.post('/user/edit',formData)
}
//活码域名列表接口
export function fallList(formData){
  return request.get('/custom_domain_list',{params:formData})
}
//活码域名保存接口
export function editDomainApi(id,formData){
  return request.post(`/domain/update/${id}`,formData)
}
//活码域名添加接口
export function addliveCodeApi(formData){
  return request.post('/custom_domain_add',formData)
}
//活码域名删除接口
export function delliveCodeApi(formData){
  return request.post(`/custom_domain_del`,formData)
}
//添加自定义域名接口
export function addDomainApi(formData){
  return request.post('/custom_domain_add',formData)
}
//删除自定义域名接口
export function delDomainApi(formData){
  return request.post('/custom_domain_del',formData)
}
// 通知中心列表接口
export function noticeListApi(formData){
  return request.get('/notice_list',formData)
}
// 通知中心列表接口
export function noticesApi(formData){
  return request.get('/notice_by_id',{params:formData})
}
//自定义域名列表接口
export function domainListApi(formData){
  return request.get('/custom_domain_list',{params:formData})
}
//旧码域名列表接口
export function oldDomainListApi(formData){
  return request.post('/custom_domain_list',formData)
}
//活码相关接口
// 创建
export function addLiveCode(formData){
  return request.post('/live_code',formData)
}
//获取
export function getliveCodeList(query) {
  return request.get('/live_code', { params: query })
}
//更新
export function updateliveCode(formData){
  return request.post(`/live_code/update/${formData.id}`,formData)
}
//删除
export function deleteliveCode(formData){
  return request.post('/live_code/delete',formData)
}
//更换入口域名
export function changeLiveCodeDomain(formData){
  return request.post('/live_code/cut',formData)
}
//二维码相关接口
//获取
export function getCodeList(query){
  return request.get('/qr_code', { params: query })
}
//创建
export function addCode(formData){
  return request.post('/qr_code',formData,{timeout:0})
}
//更新
export function updateCode(formData){
  return request.post(`/qr_code/update/${formData.id}`,formData)
}
//删除
export function deleteCode(formData){
  return request.post(`/qr_code/delete`,formData)
}
// 短链接口
// 获取短链
export function getShort(formData){
  return request.get('/short',{params:formData})
}
// 获取所有短链
export function getAllShort(){
  return request.get('/get_all_short')
}
// 创建短链
export function addShort(formData){
  return request.post('/short',formData)
}
// 编辑短链
export function editShort(formData){
  return request.post(`/short/update/${formData.id}`,formData)
}
// 删除短链
export function delShort(formData){
  return request.post('/short/delete',formData)
}
// 切换域名
export function switchDomain(formData){
  return request.post('/short/cut',formData)
}
// 检测自定义短链是否存在
export function checkShort(formData){
  return request.get('/short/check',{params:formData})
}
// 拖拽分组
export function groupSort(formData){
  return request.post('/sort',{ids:formData})
}
// 短链移动分组
export function batchChange(formData){
  return request.post('/batch_change',formData)
}
// 获取旧码列表
export function getOldCodeList(formData){
  return request.get('/other_code',{params:formData})
}
//添加旧码及分组
export function addOldCode(formData){
  return request.post('/other_code',formData)
}
//修改旧码及分组
export function editOldCode(id,formData){
  return request.post(`other_code/update/${id}`,formData)
}
//删除旧码及分组
export function delOldCode(formData){
  return request.post(`other_code/delete`,formData)
}
// 回收站活码删除
export function rbLiveCodeDel(formData){
  return request.post('/delete/liveCode',formData)
}
// 回收站活码恢复
export function rbLiveRecover(formData){
  return request.post('/back/liveCode',formData)
}
// 回收站活码列表
export function rbLiveList(formData){
  return request.get('/live_code?soft_delete=1',{params:formData})
}
// 回收站短链删除
export function rbShortDel(formData){
  return request.post('/delete/short',formData)
}
// 回收站短链恢复
export function rbShortRecover(formData){
  return request.post('/back/short',formData)
}
// 回收站短链列表
export function rbShortList(formData){
  return request.get('/short?soft_delete=1',{params:formData})
}
// 短链数据统计
export function shortStatistics(formData){
  return request.get('/short_url_data',{params:formData})
}
// 活码数据统计
export function liveCodeStatistics(formData){
  return request.get('/code_data',{params:formData})
}
// 根据id获取数据统计
export function get_data_statistics_by_id(formData){
  return request.get('/get_data_statistics_by_id',{params:formData})
}
// 通知账户列表
export function noticeSetList(formData){
  return request.get('/notice_setting_list',{params:formData})
}
// 添加通知账户
export function addNotice(formData){
  return request.post('/add_notice_setting',formData)
}
// 编辑通知账户
export function editNotice(formData){
  return request.post('/edit_notice_setting',formData)
}
// 删除通知账户
export function delNotice(formData){
  return request.get('/del_notice_setting',{params:formData})
}
// 是否通知全体开关
export function editIsAtAll(formData){
  return request.get('/edit_is_at_all',{params:formData})
}
// 监控系统列表
export function taskList(formData){
  return request.get('/api/tasklist',{params:formData})
}
// 监控日志列表
export function taskLog(formData){
  return request.get('/api/tasklog',{params:formData})
}
// 添加监控任务
export function taskAdd(formData){
  return request.post('/api/taskadd',formData)
}
// 编辑监控任务
export function taskEdit(formData){
  return request.post('/api/taskedit',formData)
}
// 测试任务
export function cloudTask(formData){
  return request.post('/api/cloudtask',formData)
}
// 启用选中
export function selectstart(formData){
  return request.get('/api/selectstart',{params:formData})
}
// 删除选中
export function selectDel(formData){
  return request.get('/api/selectdel',{params:formData})
}
// 停用选中
export function selectStop(formData){
  return request.get('/api/selectstop',{params:formData})
}
// 获取所有通知方式
export function getAllSettingList(){
  return request.get('/get_all_setting_list')
}
// 获取检测api列表
export function apilist(){
  return request.get('/apilist')
}
// 发送短信配置信息
export function sendMessageIndex(){
  return request.get('/send_message_index')
}
// 发送邮件配置信息
export function taskMailIndex(){
  return request.get('/task_mail_index')
}
// 保存短信配置
export function sendMessageSave(formData){
  return request.post('/send_message_save',formData)
}
// 保存邮件配置
export function taskMailSave(formData){
  return request.post('/task_mail_save',formData)
}
// 获取域名检测额度
export function getUserData(){
  return axios.get(`${store.state.userStore.jcApiUrl}/users/userdata?token=${store.state.userStore.token}`)
}
// 获取首页数据
export function getIndexData(){
  return request.get('/index_data')
}
// 获取语雀更新日志
export function getYuQueLog(){
  return request.get('/qulist_get')
}
// 综合检测
export function getMonitorRel(formData){
  return request.post('/get_monitor_rel',formData)
}
// 检测自定义域名是否生效
export function judgeIsEffective(formData){
  return request.get('/judge_is_effective',{ params: formData })
}
